# -*- coding: utf-8 -*-
"""
Created on Tue Jun 11 11:24:36 2024

@author: 29865
"""

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

date_table = pd.read_excel('TRD_Cale.xlsx')
value_table = pd.read_excel('IDX_Idxtrd.xlsx')

# 获取每周的开始日期和结束日期列表
week_start_list = ['2017-01-03']
week_end_list = []

for i in range(1, len(date_table) - 1):
    j = i + 1
    week_1 = date_table.iloc[i, 2]
    week_2 = date_table.iloc[j, 2]
    if week_1 > week_2:
        week_end_list.append(date_table.iloc[i, 1])
        week_start_list.append(date_table.iloc[j, 1])

week_end_list.append('2017-12-29')

# 计算周收益率并绘图
X = []
Y = []
for i in range(len(week_start_list)):
    start_value = value_table.loc[value_table['Idxtrd01'].values == week_start_list[i], 'Idxtrd05'].values
    end_value = value_table.loc[value_table['Idxtrd01'].values == week_end_list[i], 'Idxtrd05'].values
    week_yield = ((end_value - start_value) / start_value)[0]
    X.append(i + 1)
    Y.append(week_yield)

X = np.array(X)
Y = np.array(Y)
plt.rcParams['font.sans-serif'] = 'SimHei'  # 设置字体为SimHei
plt.rcParams['axes.unicode_minus'] = False  # 解决负号“-”显示异常
plt.title('周收益率变化曲线')
plt.plot(X, Y)